package com.zuoqi.easywork.userManage.service;

import com.zuoqi.common.utils.IMap;
import com.zuoqi.easywork.system.service.OpLogService;
import com.zuoqi.easywork.userManage.mapper.TcUserMapper;
import com.zuoqi.easywork.userManage.model.TcUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserService {
    @Autowired
    private TcUserMapper tcUserMapper;
    @Autowired
    private OpLogService opLogService;
    /**
     * 修改密码
     *
     * @return
     */
    public int updateUserPwd(String userId,String oldPassword,String pwd){
        return tcUserMapper.updateUserPwd(userId, oldPassword, pwd);
    }

    /**
     * 获取用户所在部门
     * @return
     */
    public IMap getUserDp(String userId){
        return tcUserMapper.getUserDp(userId);
    }

    public List<IMap> getUserListForManage(IMap param){
        return tcUserMapper.getUserListForManage(param);
    }

    public int addUser(IMap param){
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("userId"));
        tcUser.setUserName(param.getString("userName"));
        tcUser.setPassword("123456");
        tcUser.setSex(param.getString("sex"));
        tcUser.setUserPhone(param.getString("telephone",""));
        tcUser.setDepartId(param.getString("departId"));
        tcUser.setRoleId(param.getString("roleId"));
        tcUser.setState("2");
        try {
            tcUserMapper.insert(tcUser);
            opLogService.addLog(param.getString("updateUser"),"新增用户","新增用户ID："+tcUser.getUserId());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    public IMap getUserInfo(String userId){
        return tcUserMapper.getUserInfo(userId);
    }
    public int uptUser(IMap param){
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("userId"));
        tcUser.setUserName(param.getString("userName"));
        tcUser.setSex(param.getString("sex"));
        tcUser.setUserPhone(param.getString("telephone",""));
        tcUser.setDepartId(param.getString("departId"));
        tcUser.setRoleId(param.getString("roleId"));
        try {
            tcUserMapper.updateByPrimaryKeySelective(tcUser);
            opLogService.addLog(param.getString("updateUser"),"修改用户信息","修改用户ID："+tcUser.getUserId());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    public int doUserIn(IMap param){
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("userId"));
        tcUser.setState("1");
        try {
            tcUserMapper.updateByPrimaryKeySelective(tcUser);
            opLogService.addLog(param.getString("updateUser"),"执行入职操作","修改用户ID："+tcUser.getUserId());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
    public int alreadyLeave(IMap param){
        TcUser tcUser = new TcUser();
        tcUser.setUserId(param.getString("userId"));
        tcUser.setState("0");
        try {
            tcUserMapper.updateByPrimaryKeySelective(tcUser);
            opLogService.addLog(param.getString("updateUser"),"执行离职操作","修改用户ID："+tcUser.getUserId());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
